#ifndef FRAMEQUEUE_H
#define FRAMEQUEUE_H

#include <vector>
#include "Mutex.h"
#include "PacketQueue.h"

extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/avutil.h"
}

using namespace std;

typedef struct Frame {
    AVFrame *frame;
    int serial;
    double pts;           /* presentation timestamp for the frame */
    double duration;      /* estimated duration of the frame */
    int64_t pos;          /* byte position of the frame in the input file */
} Frame;

#define FRAME_QUEUE_MAXSIZE 16
#define FRAME_QUEUE_SIZE FRAME_QUEUE_MAXSIZE
class FrameQueue {
public:
	FrameQueue(int maxsize = FRAME_QUEUE_SIZE, bool keeplast = true);
	~FrameQueue();

	Frame *peek();
	Frame *peekNext();
	Frame *peekLast();
	Frame *waitForWritable();
	Frame *waitForReadable();
	void push();
	void next();
	int unDisplayFrame();
	void wait(){m_Cond.wait();}
	void notify(){m_Cond.notify();}
	int size(){return m_Size;}
	void abort() {
		AutoLock lock(&m_Cond);
		m_AbortRequest = true;
		notify();
	}
	void clear();
protected:
	Mutex m_Mutex;
	Condition m_Cond;
	Frame m_Queue[FRAME_QUEUE_SIZE];
	int m_RIndex;
	int m_Windex;
	int m_Size;
	int m_MaxSize;
	bool m_KeepLast;
	int m_RIndexShown;
	PacketQueue *m_PktQueue;
	bool m_AbortRequest;
};

#endif